package com.pdd.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.mall.common.api.CommonResult;
import com.pdd.mall.entity.Product;
import com.pdd.mall.service.ProductService;
import com.pdd.mall.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 商品控制器
 */
@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private PromotionService promotionService;

    @GetMapping
    public CommonResult<Page<Product>> getProductList(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "minPrice", required = false) Double minPrice,
            @RequestParam(value = "maxPrice", required = false) Double maxPrice,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortField", required = false) String sortField,
            @RequestParam(value = "sortOrder", required = false) String sortOrder) {
        Page<Product> productList = productService.getProductList(keyword, categoryId, minPrice, maxPrice, pageNum, pageSize, sortField, sortOrder);
        return CommonResult.success(productList);
    }

    @GetMapping("/{id}")
    public CommonResult<Map<String, Object>> getProductDetail(@PathVariable Long id) {
        Product product = productService.getProductDetail(id);
        if (product == null) {
            return CommonResult.notFound("商品不存在");
        }

        // 获取商品的促销信息
        Map<String, Object> promotionInfo = promotionService.getProductPromotion(id);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("product", product);
        result.put("hasPromotion", promotionInfo.get("hasPromotion"));
        result.put("stock", product.getStock()); // 添加库存信息

        if ((Boolean) promotionInfo.get("hasPromotion")) {
            // 获取促销活动
            Object promotionObj = promotionInfo.get("promotion");
            result.put("promotion", promotionObj);

            if (promotionObj instanceof com.pdd.mall.entity.Promotion) {
                com.pdd.mall.entity.Promotion promotion = (com.pdd.mall.entity.Promotion) promotionObj;

                // 如果是秒杀活动，直接使用促销服务计算秒杀价格
                if ("flash".equals(promotion.getType())) {
                    try {
                        // 使用原价计算秒杀价格
                        BigDecimal originalPrice = product.getPrice();
                        BigDecimal flashPrice = promotionService.calculatePromotionPrice(id, originalPrice);
                        result.put("promotionPrice", flashPrice);
                        System.out.println("商品ID: " + id + " 的秒杀价格: " + flashPrice);
                    } catch (Exception e) {
                        System.err.println("计算秒杀价格失败: " + e.getMessage());
                    }
                }

                // 如果没有设置促销价格，使用计算方法
                if (!result.containsKey("promotionPrice")) {
                    BigDecimal originalPrice = product.getPrice();
                    BigDecimal promotionPrice = promotionService.calculatePromotionPrice(id, originalPrice);
                    result.put("promotionPrice", promotionPrice);
                }
            }
        }

        return CommonResult.success(result);
    }
}
